package cn.iocoder.yudao.module.dlbz.service.companyemployee;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.dlbz.controller.admin.companyemployee.vo.*;
import cn.iocoder.yudao.module.dlbz.dal.dataobject.companyemployee.CompanyEmployeeDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.dlbz.dal.mysql.companyemployee.CompanyEmployeeMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.dlbz.enums.ErrorCodeConstants.*;

/**
 * 企业员工 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class CompanyEmployeeServiceImpl implements CompanyEmployeeService {

    @Resource
    private CompanyEmployeeMapper companyEmployeeMapper;

    @Override
    public Long createCompanyEmployee(CompanyEmployeeSaveReqVO createReqVO) {
        // 插入
        CompanyEmployeeDO companyEmployee = BeanUtils.toBean(createReqVO, CompanyEmployeeDO.class);
        companyEmployeeMapper.insert(companyEmployee);
        // 返回
        return companyEmployee.getId();
    }

    @Override
    public void updateCompanyEmployee(CompanyEmployeeSaveReqVO updateReqVO) {
        // 校验存在
        validateCompanyEmployeeExists(updateReqVO.getId());
        // 更新
        CompanyEmployeeDO updateObj = BeanUtils.toBean(updateReqVO, CompanyEmployeeDO.class);
        companyEmployeeMapper.updateById(updateObj);
    }

    @Override
    public void deleteCompanyEmployee(Long id) {
        // 校验存在
        validateCompanyEmployeeExists(id);
        // 删除
        companyEmployeeMapper.deleteById(id);
    }

    private void validateCompanyEmployeeExists(Long id) {
        if (companyEmployeeMapper.selectById(id) == null) {
            throw exception(COMPANY_EMPLOYEE_NOT_EXISTS);
        }
    }

    @Override
    public CompanyEmployeeDO getCompanyEmployee(Long id) {
        return companyEmployeeMapper.selectById(id);
    }

    @Override
    public PageResult<CompanyEmployeeDO> getCompanyEmployeePage(CompanyEmployeePageReqVO pageReqVO) {
        return companyEmployeeMapper.selectPage(pageReqVO);
    }

}